import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;

/**
 * Description:
 * User:岁亮亮
 * Date:2025-03-06
 * TIME:8:45
 */
public class SearchTree{

    static class Node{
        int val;
        Node left;
        Node right;
        public Node(int val){
            this.val = val;
        }
    }
    Node root = null;
    int size = 0;

    public boolean searchNode(int val){
        Node cur = root;
        while(cur != null){
            if(cur.val < val){
                cur = cur.left;
            }
            else if(cur.val > val){
                cur = cur.right;
            }
            else {
                return true;
            }
        }
        return false;
    }

    public boolean insterNode(int val){
        Node newNode = new Node(val);
        Node cur = root;
        if(root == null){
            root = newNode;
            size++;
            return true;
        }
        Node parent = cur;
        while(cur != null){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }
            else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }
            else {
                return false;
            }
        }
        if(parent.val < val){
            parent.right = newNode;
        }
        else {
            parent.left = newNode;
        }
        size++;
        return true;
    }
    public int getSize(){
        return size;
    }
    public void print(){
        Node cur = root;
        Queue<Node> stack = new LinkedList<>();
        stack.offer(cur);
        while(!stack.isEmpty()){
            Node node = stack.poll();
            System.out.print(node.val + " ");
            if(node.left != null){
                stack.offer(node.left);
            }
            if(node.right != null){
                stack.offer(node.right);
            }
        }
    }
    public void printSort(){
        Node cur = root;
        realPrintSort(cur);
    }

    private void realPrintSort(Node root) {
        if(root == null){
            return;
        }
        realPrintSort(root.left);
        System.out.println(root.val);
        realPrintSort(root.right);
    }
    public void removeNode(int val){
        Node cur = root;
        Node parent = cur;
        while(cur.val != val){
            if(cur.val > val){
                parent = cur;
                cur = cur.left;
            }
            else{
                parent = cur;
                cur = cur.right;
            }
        }
        if(cur.left == null){
            if(cur == root){
                root = root.right;
            }
            else if(parent.left == cur){
                parent.left = cur.right;
            }
            else if(parent.right == cur){
                parent.right = cur.right;
            }
        }
        else if(cur.right == null){
            if(cur == root){
                root = root.left;
            }
            else if(parent.left == cur){
                parent.left = cur.left;
            }
            else if(parent.right == cur){
                parent.right = cur.left;
            }
        }
        else{
            Node target = cur.right;
            Node targetParent = cur;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }
            if(targetParent.left == cur){
                targetParent.left = target.right;
            }
            else {
                targetParent.right = target.right;
            }
            cur.val = target.val;

        }
    }
}

